<?php

/**
 * get message by chat_id
 * chat/[ID]         ;ID = chat_id
 */
$m = new MongoClient('mongodb://ftp.giemiebier.nl');

$db = $m->chatdb;
$user_chats = $db->message;
//echo $pagetree[2];
$cursor = $user_chats->find(["chat_id" => $pagetree[2]])->limit(20);
$cursor->sort(array('time_sent' => -1));
$count = 0;
foreach ($cursor as $document) {
    $return['messages'][] = $document;
    $count++;
}
if ($count == 0) {
    $return['error'] == "geen berichten in deze chat";
} else {
    $return['valid'] = true;
}

echo json_encode($return);
